package com.maxjay.component.metadata.core.parser;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.maxjay.common.util.json.JsonParser;
import com.maxjay.component.metadata.constant.FormatterType;
import com.maxjay.component.metadata.core.formatter.CodeFieldFormatter;
import com.maxjay.component.metadata.core.formatter.DateFieldFormatter;
import com.maxjay.component.metadata.core.formatter.FieldFormatter;
import com.maxjay.system.service.BaseServiceJdbc;

@Component("fieldFormatterParser")
public class DatabaseFieldFormatterParser extends BaseServiceJdbc implements FieldFormatterParser {

	@Override
	public FieldFormatter parserFieldFormatter(String formatterId) {
		String qryFomatterSql = "select * from T_MD_FORMATTER t where t.OBJ_ID=?";
		Map<String, Object> formatterMap = this.getJdbcTemplate().queryForMap(qryFomatterSql, formatterId);
		String formatterConf = (String)formatterMap.get("CONF"); 
		Map<String, String> formatterConfMap = JsonParser.parseJsonStr(formatterConf);
		FieldFormatter fieldFormatter = null;
		switch (FormatterType.valueOf((String)formatterMap.get("FORMATTER_TYPE"))) {
		case CODE_FORMATTER:
			fieldFormatter = new CodeFieldFormatter(formatterConfMap.get("codeTab"));
			break;
		case DATE_FORMATTER:
			fieldFormatter = new DateFieldFormatter(formatterConfMap.get("displayFormat"), formatterConfMap.get("storeFormat"));
			break;
		default:
			break;
		}
		return fieldFormatter;
	}

}
